function Game(){
	this.div = Utils.get("divGame");
	this.view = vec2(this.div.offsetWidth, this.div.offsetHeight);
	this.eng = new Engine(this.view, this.div);
	
	this.eng.ctx.font = '24px "Arial"';
	
	this.sprites = {};
	this.maps = [];
	this.map = null;
	this.keys = [];
	this.lastK = -1;
	this.mouseP = vec2(0,0);
	this.mouseClick = 0;
	
	this.loadSprites();
}

Game.prototype.loadSprites = function(){
	this.sprites.grass = this.eng.loadImage("img/grass.png", vec2(4, 13));
	this.sprites.male1 = this.eng.loadImage("img/male1.png", vec2(17, 8), vec2(0, 30));
	this.sprites.brushes = this.eng.loadImage("img/brushes.png", vec2(8, 1));
	this.sprites.rocks = this.eng.loadImage("img/roche-cube.png", vec2(4, 5));
	this.sprites.trees = this.eng.loadImage("img/trees.png", vec2(2, 6), vec2(47, 163));
	this.sprites.misc = this.eng.loadImage("img/misc.png", vec2(4, 1), vec2(0, 32));
};

Game.prototype.drawLoading = function(){
	var ctx = this.eng.ctx;
	var per = this.eng.getImagesReadyPercent(); 
	
	ctx.fillStyle = "black";
	ctx.fillRect(0,0,ctx.width,ctx.height);
	
	ctx.fillStyle = "red";
	ctx.strokeStyle = "red";
	ctx.strokeRect(ctx.width / 2 - 200, ctx.height - 100, 400, 20);
	ctx.fillRect(ctx.width / 2 - 200, ctx.height - 100, 400 * per, 20);
};

Game.prototype.getKeyPressed = function(keyCode){
	if (this.keys[keyCode] == 1){
		this.keys[keyCode] = 2;
		return true;
	}
	return false;
};

Game.prototype.getMousePressed = function(){
	if (this.mouseClick == 1){
		this.mouseClick = 2;
		return true;
	}
	return false;
};

Game.prototype.splitText = function(text, font, width){
	var ctx = this.eng.ctx;
	ctx.save();
	
	ctx.font = font;
	
	var format = [];
	var line = "";
	var split = text.split(" ");
	
	for (var i=0,len=split.length;i<len;i++){
		if (ctx.measureText(line + " " + split[i]).width < width){
			if (line != "") line += " ";
			line += split[i];
		}else{
			format.push(line);
			line = "";
			i--;
		}
	}
	
	if (line != ""){
		format.push(line);
	}
	
	ctx.restore();
	
	return format;
};

Game.prototype.newGame = function(deltaT){
	var game = this;
	game.drawLoading();
	if (game.eng.areImagesReady()){
		this.map = new Map(this, "test");
		this.maps.push(this.map);
		
		game.loop();
	}else{
		requestAnimationFrame(function(dT){ game.newGame(dT); });
	}
};

Game.prototype.loop = function(deltaT){
	var game = this;
	
	var ctx = this.eng.ctx;
	ctx.fillStyle = "black";
	ctx.fillRect(0,0,ctx.width,ctx.height);
	
	document.body.style.cursor = "default";
	this.map.loop();
	
	requestAnimationFrame(function(dT){ game.loop(dT); });
};

Utils.addEvent(window, "load", function(){
	var game = new Game();
	game.newGame(0);
	
	Utils.addEvent(window, "resize", function(a){
		var view = vec2(game.div.offsetWidth, game.div.offsetHeight);
		game.eng.canvas.width = view.a;
		game.eng.canvas.height = view.b;
		
		game.eng.ctx.width = view.a;
		game.eng.ctx.height = view.b;
		
		game.view = view;
		if (game.map != null)
			game.map.view = vec2(Math.ceil(game.view.a / 75), Math.ceil(game.view.b / 100));
	});
	
	Utils.addEvent(document, "keydown", function(e){
		if (window.event) e = window.event;
		
		if (game.keys[e.keyCode] == 2) return;
		game.keys[e.keyCode] = 1;
		game.lastK = e.keyCode;
	});
	Utils.addEvent(document, "keyup", function(e){
		if (window.event) e = window.event;
		
		game.keys[e.keyCode] = 0;
	});
	Utils.addEvent(game.eng.canvas, "mousemove", function(e){
		if (window.event) e = window.event;
		
		game.mouseP.set(e.clientX - game.div.offsetLeft, e.clientY - game.div.offsetTop);
	});
	Utils.addEvent(game.eng.canvas, "mousedown", function(e){
		if (window.event) e = window.event;
		
		if (game.mouseClick == 2) return;
		game.mouseClick = 1;
	});
	Utils.addEvent(game.eng.canvas, "mouseup", function(e){
		if (window.event) e = window.event;
		game.mouseClick = 0;
	});
});
